﻿using System;
using System.Windows.Data;

namespace Oxage.Common
{
	/// <summary>
	/// Helps binding enums to RadioButton controls in XAML.
	/// Reference: http://www.wpftutorial.net/RadioButton.html
	/// </summary>
	public class RadioButtonConverter : IValueConverter
	{
		#region IValueConverter Members
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value == null || parameter == null)
			{
				return false;
			}
			string checkValue = value.ToString();
			string targetValue = parameter.ToString();
			return checkValue.Equals(targetValue, StringComparison.InvariantCultureIgnoreCase);
		}

		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value == null || parameter == null)
			{
				return null;
			}
			bool useValue = (bool)value;
			string targetValue = parameter.ToString();
			if (useValue)
			{
				return Enum.Parse(targetType, targetValue);
			}
			return null;
		}
		#endregion
	}
}
